import $web3js from "../web3.js";

const ifilTokenContract = require("./ifilTokenContract.json");

let contractAddress = "";
let web3 = $web3js.getWeb3();
let contractConst = {
  contract: web3
    ? new web3.eth.Contract(ifilTokenContract.abi, contractAddress)
    : ""
};
const getIfilTokenAddress = () => {
  return contractAddress;
};

const getIfilTokenContract = () => {
  return contractConst.contract;
};

const getIfilAllowance = (owner, spender) => {
  return $web3js.commonCallAndHandler(
    getIfilTokenContract().methods.allowance,
    $web3js.fromWei,
    owner,
    spender
  );
};

const ifilApprove = (address, value) => {
  value = $web3js.toWei(value);
  return $web3js.commonSend(
    getIfilTokenContract().methods.approve,
    address,
    value
  );
};

const ifilApproveEstimateGas = (address, value) => {
  value = $web3js.toWei(value);
  return $web3js.commonEstimateGas(
    getIfilTokenContract().methods.approve,
    address,
    value
  );
};

const getIfilBalance = address => {
  return $web3js.commonCallAndHandler(
    getIfilTokenContract().methods.balanceOf,
    $web3js.fromWei,
    address
  );
};

export default {
  ifilApprove,
  ifilApproveEstimateGas,
  getIfilAllowance,
  getIfilBalance,
  getIfilTokenAddress,
  getIfilTokenContract
};
